<template>
    <div class="cmsCat">
        <div class="butons">
            <el-button icon="el-icon-plus" @click="addCmsCat">添加</el-button>
            <el-button icon="el-icon-delete" @click="confirmDelete">删除</el-button>
            <el-button @click="asyncJdCms">同步京东CMS信息</el-button>
            <el-input style="flex: 1; margin-left: 300px" placeholder="请输入名称搜索" prefix-icon="el-icon-search" v-model="searchKey">
            </el-input>
        </div>
        <div class="cmsCatTable">
            <el-skeleton style="width: 100%; margin-top: 15px; padding: 0px 10px" v-if="!isShowTable" :rows="15" animated />
            <el-table v-if="isShowTable" ref="brandTable" tooltip-effect="dark" :data="brandData">
                <el-table-column type="selection"> </el-table-column>
                <el-table-column prop="name" label="CMS分类名称"> </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button icon="el-icon-edit" @click="updateCmsCat(scope.$index, scope.row)">编辑
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination v-if="isShowTable" @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[5, 10, 100, 200, 300, 400]" :page-size="10" layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </div>
        <add-cms-cat @close="close" v-if="isShowCmsCat" />
    </div>
</template>
<script lang="ts">
    import { Component, Vue, Watch } from "vue-property-decorator";
    import { namespace } from "vuex-class";
    import AdminApiActions from "@/components/api/admin-api-actions";
    import $ from "jquery";
    const goodsName = namespace("goods");
    import AddCmsCat from "@/components/admin-manager/cms-index-manager/cms-dialog/add-cms-cat-dialog.vue";
    @Component({
        components: {
            AddCmsCat,
        },
    })
    export default class ProductBrand extends Vue {
        @goodsName.Mutation updateStatus;
        public total: any = 0;
        public searchKey: any = "";
        public brandData: Array < any > = new Array < any > ();
        public brandDialogVisible: boolean = false;
        public formLabelWidth: any = "120px";
        public params: any = {};
        public currentPage: number = 1;
        public pageSize: number = 100;
        public isShowCmsCat: boolean = false;
        public dialogStyle: any = {};
        public isShowTable: boolean = false;
        public scope: any = "";
        //创建时调用
        async created() {
            await this.initPageData();
            this.$nextTick(() => {
                $(".el-table th.el-table_1_column_1>.cell").css({
                    "padding-left": "14px!important",
                });
            });
        }
        //分页发生变化
        public async handleSizeChange(pageSize) {
            this.pageSize = pageSize;
            await this.initPageData();
        }
        //当前页发生变化
        public async handleCurrentChange(currentPage) {
            this.currentPage = currentPage;
            await this.initPageData();
        }
        public async initPageData() {
            let apiActions = new AdminApiActions(this);
            let result = await apiActions.getCmsCategoryPage({
                name: "",
                pager: { pageSize: this.pageSize, currentPage: this.currentPage },
            });
            this.brandData = result.data.rows;
            this.total = result.data.total;
            this.isShowTable = true;
        }
        //添加分组
        public addCmsCat() {
            this.params.type = "add";
            this.isShowCmsCat = true;
        }
        //编辑分组
        public updateCmsCat(index, item) {
            this.params.type = "update";
            this.isShowCmsCat = true;
        }
        public confirmDelete() {
            let brandTable: any = this.$refs.brandTable;
            let selectData: Array < any > = brandTable.selection;
            if (selectData.length == 0) {
                this.$alert("请选择数据", "提示", {
                    confirmButtonText: "确定",
                });
                return false;
            }
            this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                })
                .then(async () => {
                    await this.deleteCmaCat();
                    this.$message({
                        type: "success",
                        message: "删除成功!",
                        center: true,
                    });
                })
                .catch(() => {});
        }
        //删除分组品牌
        public async deleteCmaCat() {
            let brandTable: any = this.$refs.brandTable;
            let selectData: Array < any > = brandTable.selection;
            let apiActions = new AdminApiActions(this);
            let ids: any = selectData.map((item) => {
                return item.id;
            });
            await apiActions.deleteCmsCategory(ids);
            await this.initPageData();
        }
        //关闭
        public close() {
            this.isShowCmsCat = false;
        }
        //确认数据
        public async confirm() {
            this.brandDialogVisible = false;
            await this.initPageData();
        }

        @Watch("searchKey")
        public async searchByKey(searchText: any) {
            let apiActions = new AdminApiActions(this);
            await this.initPageData();
        }

        public async asyncJdCms() {
            let apiActions = new AdminApiActions(this);
            await apiActions.asyncJdCmsData();
        }
        public centerDialog() {
            this.$nextTick(() => {
                let height = $(window).height();
                let dialogHeight = $(".relatedCatagory .el-dialog").height();
                this.dialogStyle = {
                    top: height / 2 - dialogHeight / 2 + "px",
                };
            });
        }
    }
</script>
<style lang="less">
    .cmsCat {
        .relatedCatagory {
            .el-table {
                height: 300px;
            }
        }

        width: 100%;
        height: 100%;

        .el-dialog {
            width: 40%;
            margin-top: 0px !important;

            .dialog-footer {
                display: flex;
                justify-content: center;
            }
        }

        .el-table__body-wrapper {
            overflow-y: auto;
            height: calc(100% - 70px);
        }

        .has-gutter .el-table-column--selection .cell {
            padding-left: 14px !important;
        }

        border: soild 1px red;

        .butons {
            display: flex;
            flex-direction: row;
        }

        .cmsCatTable {
            height: calc(100% - 40px);

            .el-table {
                height: calc(100% - 70px);
            }

            .el-pagination {
                margin-top: 12px;
            }
        }
    }
</style>